package com.molichuxing.gateway.bff.orders.convertor;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.utils.BigDecimalUtils;
import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.gateway.bff.orders.entity.request.create.OrderCreateVo;
import com.molichuxing.gateway.bff.orders.entity.request.create.OrderPayeeCreateVo;
import com.molichuxing.gateway.bff.orders.entity.request.modify.*;
import com.molichuxing.gateway.bff.orders.entity.response.*;
import com.molichuxing.gateway.bff.user.entity.response.UserVo;
import com.molichuxing.gateway.contants.OrderConst;
import com.molichuxing.gateway.property.OrderButtonEnum;
import com.molichuxing.gateway.utils.OrderUtil;
import com.molichuxing.services.business.dto.request.create.*;
import com.molichuxing.services.business.dto.request.modify.*;
import com.molichuxing.services.business.dto.response.*;
import com.molichuxing.services.contants.OrderStatusConst;
import com.molichuxing.services.infrastructure.dto.request.create.UserPayeeCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.UserPayeeModifyDto;
import com.molichuxing.services.infrastructure.dto.response.CarDto;
import com.molichuxing.services.infrastructure.dto.response.*;
import com.molichuxing.services.property.*;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Convert {

	public static OrderMobileListVo toOrderMobileListVo(OrderMemberBizDto beanBizDto) {
		OrderMobileListVo beanVo = null;

		if (beanBizDto != null) {
			beanVo = new OrderMobileListVo();
			BeanUtils.copyProperties(beanBizDto, beanVo);

			// 下单门店id
			beanVo.setStoreId(beanBizDto.getPlaceStoreId());
			// 服务器当前时间
			beanVo.setCurrentDate(LocalDateTime.now());
			// 订单过期时间
			beanVo.setExpiryDate(
					DateUtil.plus(beanBizDto.getOrderTime(), OrderConst.ORDER_VALID_TIME, ChronoUnit.MINUTES));
			// 订单类型
			beanVo.setBusinessType(beanBizDto.getType().getValue());
			if (OrderTypeEnum.EXCLUSIVEL.equals(beanBizDto.getType())) {
				// 专属
				OrderExclusivelBizDto exclusivelBizDto = (OrderExclusivelBizDto) beanBizDto;
				// 订单状态
				beanVo.setOrderStatus(exclusivelBizDto.getOrderStatus().getValue());
				beanVo.setOrderStatusStr(OrderUtil.transformStatus(exclusivelBizDto.getOrderStatus(),
						exclusivelBizDto.getRenege() == null ? null : exclusivelBizDto.getRenege().getCostType()));
				// 规格信息 ：用车方案（回购/不回购）/租期（XX期）/付款方式 （全款/月付）
				beanVo.setSpecific((exclusivelBizDto.getIsBuyback() ? "回购" : "不回购") + " / "
						+ exclusivelBizDto.getTransportTime() + "期 / "
						+ (OrderPaymentMethodEnum.INSTALLMENT.equals(exclusivelBizDto.getPaymentMethod()) ? "月付"
						: exclusivelBizDto.getPaymentMethod().getName()));
				if (OrderStatusConst.RENEGE_EXCLUSIVEL_STATUS.contains((exclusivelBizDto.getOrderStatus()))) {
					beanVo.setPayType(PaymentCostTypeEnum.RENEGE.getValue());
				}

			} else if (OrderTypeEnum.SUBSCRIBE.equals(beanBizDto.getType())) {
				// 订阅
				OrderSubscribeBizDto subscribeBizDto = (OrderSubscribeBizDto) beanBizDto;
				// 订单状态
				beanVo.setOrderStatus(subscribeBizDto.getOrderStatus().getValue());
				beanVo.setOrderStatusStr(subscribeBizDto.getOrderStatus().getName());
				// 规格信息 ：用车时长 /基础里程，订阅里程
				beanVo.setSpecific(subscribeBizDto.getTransportTime() + "个月 / " + subscribeBizDto.getBaseMileage()
						+ "公里, " + subscribeBizDto.getPackageMileage() + "公里");
			}
		}

		return beanVo;
	}

	public static OrderExclusivelDetailsVo toOrderExclusivelDetailsVo(OrderExclusivelDetailsBizDto beanDto) {
		OrderExclusivelDetailsVo beanVo = null;
		if (beanDto != null) {
			beanVo = new OrderExclusivelDetailsVo();
			BeanUtils.copyProperties(beanDto, beanVo);
			// 订单状态
			beanVo.setOrderStatus(beanDto.getOrderStatus().getValue());
			beanVo.setOrderStatusStr(OrderUtil.transformStatus(beanDto.getOrderStatus(),
					beanDto.getRenege() == null ? null : beanDto.getRenege().getCostType()));
			// 服务器当前时间
			beanVo.setCurrentDate(LocalDateTime.now());
			// 订单过期时间
			beanVo.setExpiryDate(
					DateUtil.plus(beanDto.getOrderTime(), OrderConst.ORDER_VALID_TIME, ChronoUnit.MINUTES));
			// 规格信息 ：用车方案（回购/不回购）/租期（XX期）/付款方式 （全款/月付）
			String paymentMethodStr = OrderPaymentMethodEnum.INSTALLMENT.equals(beanDto.getPaymentMethod()) ? "月付"
					: beanDto.getPaymentMethod().getName();
			beanVo.setSpecific((beanDto.getIsBuyback() ? "回购" : "不回购") + " / " + beanDto.getTransportTime() + "期 / "
					+ paymentMethodStr);
			// 租期信息
			beanVo.setPeriodsExplain("租期" + beanDto.getTransportTime() + "期："
					+ DateUtil.formatDate(beanDto.getLeaseInception()) + " （周"
					+ DateUtil.getWeek(beanDto.getLeaseInception()) + "）至 "
					+ DateUtil.formatDate(beanDto.getLeaseExpire()) + "（周" + DateUtil.getWeek(beanDto.getLeaseExpire())
					+ "）");
			// 付款方式
			beanVo.setPaymentMethod(beanDto.getPaymentMethod().getValue());
			// 下单数据
			beanVo.setOrderDatas(Arrays.asList(new DataVo("订单编号", beanDto.getOrderCode() + ""),
					new DataVo("下单时间", DateUtil.formatTime(beanDto.getOrderTime()))));
			if (!OrderExclusivelStatusEnum.TRADE_UNPAID.equals(beanDto.getOrderStatus())
					&& !OrderExclusivelStatusEnum.TRADE_CANCELED.equals(beanDto.getOrderStatus())) {
				// 首付信息
				beanVo.setPayDatas(Arrays.asList(new DataVo("支付方式", OrderUtil.transUserPayWay(beanDto.getPayWay())),
						new DataVo("支付时间", DateUtil.formatTime(beanDto.getPayTime()))));
			}
			// 取还车数据
			if (beanDto.getDeliveryd() != null) {
				beanVo.setCarFlowDatas(
						Arrays.asList(
								new DataVo("提车时间", DateUtil.formatTime(beanDto.getDeliveryd().getDeliveryTime()))));
			}

			if (OrderStatusConst.RENEGE_EXCLUSIVEL_STATUS.contains((beanDto.getOrderStatus()))) {
				beanVo.setPayType(PaymentCostTypeEnum.RENEGE.getValue());
			}

			// 提车须知
			if (OrderExclusivelStatusEnum.TRADE_UNPAID.equals(beanDto.getOrderStatus())
					|| OrderExclusivelStatusEnum.TRADE_UNAUDIT.equals(beanDto.getOrderStatus())
					|| OrderExclusivelStatusEnum.TRADE_UNDELIVERY.equals(beanDto.getOrderStatus())) {
				beanVo.setDeliveryNotice(true);
			} else {
				beanVo.setDeliveryNotice(false);
			}
		}

		return beanVo;
	}

	public static ScheduleVo toScheduleVo(OrderScheduleDto beanDto) {
		ScheduleVo beanVo = null;
		if (beanDto != null) {
			beanVo = new ScheduleVo();
			BeanUtils.copyProperties(beanDto, beanVo);
			// 排期id
			beanVo.setScheduleId(beanDto.getId());
			// 期数
			beanVo.setPeriodsStr(beanDto.getPeriods() + "期");
			// 状态
			beanVo.setStatus(beanDto.getStatus().getValue());
			// 默认未逾期
			beanVo.setIsOverdue(false);
			// 判断是否逾期
			if (OrderScheduleStatusEnum.ALREADY.equals(beanDto.getStatus())) {
				// 已还款
				if (DateUtil.getDaysDiff(beanDto.getPayableDate(), beanDto.getPaySuccessTime()) > 0) {
					// 逾期
					beanVo.setIsOverdue(true);
					// 状态 str
					beanVo.setStatusStr(beanDto.getStatus().getName());
				}
				// 支付方式
				beanVo.setPayWay(beanDto.getPayWay().getValue());
				beanVo.setPayWayStr(OrderUtil.transUserPayWay(beanDto.getPayWay()));
			} else if (OrderScheduleStatusEnum.NO.equals(beanDto.getStatus())) {
				// 未还款
				if (DateUtil.getDaysDiff(beanDto.getPayableDate(), LocalDate.now()) > 0) {
					// 逾期
					beanVo.setIsOverdue(true);
					// 状态 str
					beanVo.setStatusStr("已逾期");
				}
			}
		}

		return beanVo;
	}

	public static OrderSubscribeDetailsVo toOrderSubscribeDetailsVo(OrderSubscribeDetailsBizDto beanDto) {
		OrderSubscribeDetailsVo beanVo = null;
		if (beanDto != null) {
			beanVo = new OrderSubscribeDetailsVo();
			BeanUtils.copyProperties(beanDto, beanVo);
			// 订单状态
			beanVo.setOrderStatus(beanDto.getOrderStatus().getValue());
			beanVo.setOrderStatusStr(beanDto.getOrderStatus().getName());
			// 服务器当前时间
			beanVo.setCurrentDate(LocalDateTime.now());
			// 订单过期时间
			beanVo.setExpiryDate(
					DateUtil.plus(beanDto.getOrderTime(), OrderConst.ORDER_VALID_TIME, ChronoUnit.MINUTES));
			// 规格信息（用车时长/基础里程，里程包里程）
			beanVo.setSpecific(beanDto.getTransportTime() + "个月/" + beanDto.getBaseMileage() + "公里,"
					+ beanDto.getPackageMileage() + "公里");
			// 取车说明
			beanVo.setDeliveryExplain("取 " + DateUtil.formatDate(beanDto.getLeaseInception()) + "（周"
					+ DateUtil.getWeek(beanDto.getLeaseInception()) + "）");
			beanVo.setDeliveryExplainStr(beanDto.getDeliveryd() == null ? "到店办理提车" : "已办理提车");
			// 还车说明
			beanVo.setReturnExplain("还 " + DateUtil.formatDate(beanDto.getLeaseExpire()) + "（周"
					+ DateUtil.getWeek(beanDto.getLeaseExpire()) + "）");
			if (OrderSubscribeStatusEnum.RETURN_FINISH.equals(beanDto.getOrderStatus())
					&& beanDto.getIsHaveSuborder()) {
				beanVo.setReturnExplainStr(null);
			} else {
				beanVo.setReturnExplainStr(beanDto.getReturnd() == null ? "到店办理还车" : "已办理还车");
			}
			// 下单数据
			beanVo.setOrderDatas(Arrays.asList(new DataVo("订单编号", beanDto.getOrderCode() + ""),
					new DataVo("下单时间", DateUtil.formatTime(beanDto.getOrderTime()))));

			if (!OrderSubscribeStatusEnum.TRADE_UNPAID.equals(beanDto.getOrderStatus())
					&& !OrderSubscribeStatusEnum.TRADE_CANCELED.equals(beanDto.getOrderStatus())) {
				// 支付数据
				beanVo.setPayDatas(Arrays.asList(new DataVo("支付方式", OrderUtil.transUserPayWay(beanDto.getPayWay())),
						new DataVo("支付时间", DateUtil.formatTime(beanDto.getPayTime()))));
			}
			// 取还车数据
			List<DataVo> carFlowDatas = null;
			if (beanDto.getDeliveryd() != null) {
				carFlowDatas = new ArrayList<>(2);
				carFlowDatas.add(new DataVo("提车时间",
						DateUtil.formatTime(beanDto.getDeliveryd().getDeliveryTime())));
			}
			if (beanDto.getReturnd() != null) {
				carFlowDatas = carFlowDatas == null ? new ArrayList<>(1) : carFlowDatas;
				carFlowDatas.add(new DataVo("还车时间",
						DateUtil.formatTime(beanDto.getReturnd().getReturnTime())));
			}
			beanVo.setCarFlowDatas(carFlowDatas);
			// 加购数据
			if (beanDto.getPurchase() != null && beanDto.getPurchase().getDetails() != null
					&& !beanDto.getPurchase().getDetails().isEmpty()) {
				List<List<DataVo>> purchasedDatas = new ArrayList<>(beanDto.getPurchase().getDetails().size());
				for (OrderMileageBizDto orderMileageBizDto : beanDto.getPurchase().getDetails()) {
					List<DataVo> purchasedData = null;
					if (OrderMileagePayStatusEnum.WAIT.equals(orderMileageBizDto.getStatus())) {
						// 未支付
						purchasedData = Arrays
								.asList(new DataVo("加购时间",
										DateUtil.formatTime(orderMileageBizDto.getPurchaseTime())));
					} else if (OrderMileagePayStatusEnum.PREPAID.equals(orderMileageBizDto.getStatus())) {
						// 已支付
						purchasedData = Arrays
								.asList(new DataVo("加购时间",
										DateUtil.formatTime(orderMileageBizDto.getPurchaseTime())),
										new DataVo("支付方式", OrderUtil.transUserPayWay(orderMileageBizDto.getPayWay())),
										new DataVo("支付时间", DateUtil.formatTime(orderMileageBizDto.getPayTime())));
					} else {
						continue;
					}
					purchasedDatas.add(purchasedData);
				}
				beanVo.setPurchasedDatas(purchasedDatas);
			}

			// 提车须知
			if (OrderSubscribeStatusEnum.TRADE_UNPAID.equals(beanDto.getOrderStatus())
					|| OrderSubscribeStatusEnum.TRADE_UNAUDIT.equals(beanDto.getOrderStatus())
					|| OrderSubscribeStatusEnum.TRADE_UNDELIVERY.equals(beanDto.getOrderStatus())) {
				beanVo.setDeliveryNotice(true);
			} else {
				beanVo.setDeliveryNotice(false);
			}
		}
		return beanVo;
	}

	/**
	 * 专属订单费用明细
	 * 
	 * @param orderExclusivelDetailsBizDto
	 * @param orderScheduleDtos
	 * @return
	 */
	public static List<CostDetailsVo> toCostDetails(OrderExclusivelDetailsBizDto orderExclusivelDetailsBizDto,
			List<OrderScheduleDto> orderScheduleDtos) throws BizException {
		List<CostDetailsVo> costs = new ArrayList<>(2);
		List<CostDetailsVo> childCosts = null;
		if (OrderPaymentMethodEnum.INSTALLMENT.equals(orderExclusivelDetailsBizDto.getPaymentMethod())) {
			childCosts = new ArrayList<>(2);
			// 分期
			if (OrderExclusivelStatusEnum.TRADE_UNPAID.equals(orderExclusivelDetailsBizDto.getOrderStatus())) {
				// 待支付
				childCosts.add(new CostDetailsVo("本次支付", "1期", orderExclusivelDetailsBizDto.getRental()));
				childCosts.add(new CostDetailsVo("待还", (orderExclusivelDetailsBizDto.getTransportTime() - 1) + "期",
						orderExclusivelDetailsBizDto.getCartFee().subtract(orderExclusivelDetailsBizDto.getRental())));
			} else {
				// 已还次数、待还次数
				Integer hasPayPeriods = 0, waitPayPeriods = 0;
				// 已还金额、待还金额
				BigDecimal hasPayAmount = BigDecimal.ZERO, waitPayAmount = BigDecimal.ZERO;
				for (OrderScheduleDto orderScheduleDto : orderScheduleDtos) {
					if (OrderScheduleStatusEnum.ALREADY.equals(orderScheduleDto.getStatus())) {
						// 已还
						hasPayPeriods += 1;
						hasPayAmount = hasPayAmount.add(orderScheduleDto.getPayable());
					} else {
						// 待还
						waitPayPeriods += 1;
						waitPayAmount = waitPayAmount.add(orderScheduleDto.getPayable());
					}
				}
				childCosts.add(new CostDetailsVo("已还", hasPayPeriods + "期", hasPayAmount));
				childCosts.add(new CostDetailsVo("待还", waitPayPeriods + "期", waitPayAmount));
			}
			costs.add(new CostDetailsVo("租车费", null, null, childCosts));
		} else {
			// 全款
			costs.add(new CostDetailsVo("租车费", null, orderExclusivelDetailsBizDto.getCartFee(), childCosts));
		}
		costs.add(new CostDetailsVo("保证金", orderExclusivelDetailsBizDto.getDeposit()));

		return costs;
	}

	/**
	 * 订阅订单费用明细
	 * 
	 * @param orderSubscribeDetailsBizDto
	 * @return
	 */
	public static List<CostDetailsVo> toCostDetails(OrderSubscribeDetailsBizDto orderSubscribeDetailsBizDto) {
		List<CostDetailsVo> costs = new ArrayList<>();
		costs.add(new CostDetailsVo("租车费", orderSubscribeDetailsBizDto.getTransportTime() + "个月",
				orderSubscribeDetailsBizDto.getRental()));
		costs.add(new CostDetailsVo("基础里程费", orderSubscribeDetailsBizDto.getBaseMileage() + "公里",
				orderSubscribeDetailsBizDto.getBaseMileagePrice()));
		costs.add(new CostDetailsVo("订阅里程费", orderSubscribeDetailsBizDto.getPackageMileage() + "公里",
				orderSubscribeDetailsBizDto.getPackageMileagePrice()));
		// 加购
		if (orderSubscribeDetailsBizDto.getPurchase() != null
				&& orderSubscribeDetailsBizDto.getPurchase().getDetails() != null
				&& !orderSubscribeDetailsBizDto.getPurchase().getDetails().isEmpty()) {
			for (OrderMileageBizDto orderMileageBizDto : orderSubscribeDetailsBizDto.getPurchase().getDetails()) {
				if (!OrderMileagePayStatusEnum.PREPAID.equals(orderMileageBizDto.getStatus())) {
					// 未支付，跳过
					continue;
				}
				if (orderMileageBizDto.getBeyondFee() != null
						&& orderMileageBizDto.getBeyondFee().compareTo(BigDecimal.ZERO) > 0) {
					costs.add(new CostDetailsVo("超里程费用",
							orderMileageBizDto.getBeyondMileage() + "公里x"
									+ BigDecimalUtils.toString(orderMileageBizDto.getBeyondMileageUnitPrice()) + "元",
							orderMileageBizDto.getBeyondFee()));
				}
				costs.add(new CostDetailsVo("订阅里程费（加购）", orderMileageBizDto.getPurchaseMileage() + "公里",
						orderMileageBizDto.getPurchaseFee()));
			}
		}
		// 结算
		if (orderSubscribeDetailsBizDto.getSettlement() != null) {
			OrderSubscribeSettlementBizDto settlement = orderSubscribeDetailsBizDto.getSettlement();
			if (settlement.getBeyondMileageFee() != null
					&& settlement.getBeyondMileageFee().compareTo(BigDecimal.ZERO) > 0) {
				costs.add(new CostDetailsVo("超里程费用",
						settlement.getBeyondMileage() + "公里x"
								+ BigDecimalUtils.toString(settlement.getBeyondMileageUnitPrice()) + "元/公里",
						settlement.getBeyondMileageFee()));
			}
			if (settlement.getBeyondDayFee() != null && settlement.getBeyondDayFee().compareTo(BigDecimal.ZERO) > 0) {
				costs.add(new CostDetailsVo("超时费用",
						settlement.getBeyondDay() + "天x" + BigDecimalUtils.toString(settlement.getBeyondDayUnitPrice())
								+ "元/天",
						settlement.getBeyondDayFee()));
			}
			if (settlement.getDamageFee() != null && settlement.getDamageFee().compareTo(BigDecimal.ZERO) > 0) {

				costs.add(new CostDetailsVo("质损费用", settlement.getDamageFee()));
			}
		}
		if (orderSubscribeDetailsBizDto.getGiftMileage() != null && orderSubscribeDetailsBizDto.getGiftMileage() > 0) {
			// 赠送里程
			Integer giftFee = orderSubscribeDetailsBizDto.getGiftMileage() * OrderConst.GIFT_UNIT_PRICE;
			CostDetailsVo costDetails = new CostDetailsVo("赠送里程", orderSubscribeDetailsBizDto.getGiftMileage() + "公里",
					new BigDecimal(giftFee));
			costDetails.setAmountStr("价值" + costDetails.getAmountStr());
			costs.add(costDetails);
		}
		if (orderSubscribeDetailsBizDto.getDeposit() != null
				&& orderSubscribeDetailsBizDto.getDeposit().compareTo(BigDecimal.ZERO) > 0) {
			costs.add(new CostDetailsVo("保证金", orderSubscribeDetailsBizDto.getDeposit()));
		}
		return costs;
	}

	public static ButtonVo toButton(OrderButtonEnum orderButtonEnum) {
		ButtonVo vo = new ButtonVo();
		vo.setName(orderButtonEnum.getName());
		vo.setType(orderButtonEnum.getValue());
		return vo;
	}

	public static OrderExclusivelCreateBizDto toOrderExclusivelCreateBizDto(OrderCreateVo createVo,
			GoodsOrderBizDto goods) {
		OrderExclusivelCreateBizDto createBizDto = null;
		if (createVo == null || goods == null) {
			return createBizDto;
		}
		createBizDto = new OrderExclusivelCreateBizDto();
		BeanUtils.copyProperties(goods, createBizDto);
		// 用户id
		createBizDto.setUserId(createVo.getOperateId());
		// 下单门店
		createBizDto.setPlaceStoreId(createVo.getStoreId());
		// 订单下单时间
		createBizDto.setOrderTime(LocalDateTime.now());
		// 订单来源
		createBizDto.setSource(createVo.getSource());
		// 下单设备编号
		createBizDto.setPlaceDeviceNum(createVo.getDeviceNum());
		// 下单销售顾问id
		createBizDto.setPlaceSalesmanId(createVo.getSalesmanId());
		// 商品编号
		createBizDto.setGoodsCode(goods.getCode());
		// 车型ID
		createBizDto.setVehicleId(goods.getVehicleStyleId());
		// 付款方式（1：一次性付清，2：分期）
		createBizDto.setPaymentMethod(OrderPaymentMethodEnum.getEnum(goods.getPaymentType()));
		// 租金（总租金）
		createBizDto.setCartFee(createBizDto.getRental());
		// 商品租金（分期为每期租金）
		createBizDto.setRental(OrderPaymentMethodEnum.FULL.equals(createBizDto.getPaymentMethod())
				? createBizDto.getRental()
				: createBizDto.getRental().divide(new BigDecimal(goods.getUseVehicleTime()), 2,
						BigDecimal.ROUND_HALF_UP));
		// 保证金
		createBizDto.setDeposit(goods.getBond());
		// 服务费
		createBizDto.setServiceFee(goods.getServiceFee());
		// 还款周期（1：1个月，n：n个月）
		createBizDto.setRepaymentPeriod(goods.getPaymentCycle());
		// 用车时长
		createBizDto.setTransportTime(goods.getUseVehicleTime());
		// 是否回购
		createBizDto.setIsBuyback(goods.getIsBuyBack() == 0 ? false : true);
		// 残值购买费
		createBizDto.setScrapValue(goods.getResiduaPurchaseAmount());
		// 合同模板ids，多个逗号分隔
		// createBizDto.setContractTemplateIds(contractTemplateIds);
		// 起租日
		createBizDto.setLeaseInception(LocalDate.now());
		// 截止日（起租日加租期）
		createBizDto.setLeaseExpire(LocalDate.now().plusMonths(goods.getUseVehicleTime().longValue()));
		// 限制里程
		createBizDto.setLimitMileage(goods.getLimitMileage());
		// 过户费
		createBizDto.setTransferFee(goods.getTransferFee());
		// 专属商品类型：1：专属里程，2：订悦里程
		createBizDto.setType(OrderExclusivelTypeEnum.getEnum(goods.getExclusiveType()));

		return createBizDto;
	}

	public static OrderPurchaseCreateBizDto toOrderPurchaseCreateBizDto(OrderCreateVo createVo, GoodsOrderBizDto goods,
			Integer beyondMileage) {
		OrderPurchaseCreateBizDto createBizDto = null;
		if (createVo == null || goods == null) {
			return createBizDto;
		}
		createBizDto = new OrderPurchaseCreateBizDto();
		// 主订单号 
		createBizDto.setMasterOrderCode(createVo.getOrderCode());
		// 用户id 
		createBizDto.setUserId(createVo.getOperateId());
		// 里程包id 
		createBizDto.setPackageMileageId(goods.getPackageMileageId());
		// 加购里程数 
		createBizDto.setPurchaseMileage(goods.getPackageMileage());
		// 加购里程金额  
		createBizDto.setPurchaseFee(goods.getPackageMileagePrice());
		// 超里程数 
		createBizDto.setBeyondMileage(beyondMileage);
		// 里程单价 
		createBizDto.setBeyondMileageUnitPrice(new BigDecimal(OrderConst.MILEAGE_UNIT_PRICE));
		// 超里程金额 
		createBizDto.setBeyondFee(new BigDecimal(OrderConst.MILEAGE_UNIT_PRICE * beyondMileage));
		// 加购时间
		createBizDto.setPurchaseTime(LocalDateTime.now());
		
		return createBizDto;
	}

	public static OrderSubscribeCreateBizDto toOrderSubscribeCreateBizDto(OrderCreateVo createVo,
			GoodsOrderBizDto goods) {
		OrderSubscribeCreateBizDto createBizDto = null;
		if (createVo == null || goods == null) {
			return createBizDto;
		}
		createBizDto = new OrderSubscribeCreateBizDto();
		BeanUtils.copyProperties(goods, createBizDto);
		// 用户id
		createBizDto.setUserId(createVo.getOperateId());
		// 订单下单时间
		createBizDto.setOrderTime(LocalDateTime.now());
		// 下单门店
		createBizDto.setPlaceStoreId(createVo.getStoreId());
		// 订单来源
		createBizDto.setSource(createVo.getSource());
		// 下单设备编号
		createBizDto.setPlaceDeviceNum(createVo.getDeviceNum());
		// 下单销售顾问id
		createBizDto.setPlaceSalesmanId(createVo.getSalesmanId());

		// 商品编号
		createBizDto.setGoodsCode(goods.getCode());
		// 车型ID
		createBizDto.setVehicleId(goods.getVehicleStyleId());
		// 租金（总租金）
		createBizDto.setCartFee(goods.getRental().add(goods.getBaseMileagePrice()).add(goods.getPackageMileagePrice()));
		// 保证金
		createBizDto.setDeposit(goods.getBond());
		// 服务费率
		createBizDto.setServiceFeeRate(goods.getServiceFee());
		// 付款方式（1：全款）
		createBizDto.setPaymentMethod(OrderPaymentMethodEnum.getEnum(goods.getPaymentType()));
		// 用车时长
		createBizDto.setTransportTime(goods.getUseVehicleTime());
		// 合同模板ids，多个逗号分隔
		// createBizDto.setContractTemplateIds(contractTemplateIds);
		// 起租日
		createBizDto.setLeaseInception(LocalDate.now());
		// 截止日（起租日加租期）
		createBizDto.setLeaseExpire(LocalDate.now().plusMonths(goods.getUseVehicleTime().longValue()));

		return createBizDto;
	}

	public static OrderRenewCreateBizDto toOrderRenewCreateBizDto(OrderCreateVo createVo, GoodsOrderBizDto goods) {
		OrderRenewCreateBizDto createBizDto = null;
		if (createVo == null || goods == null) {
			return createBizDto;
		}
		createBizDto = new OrderRenewCreateBizDto();
		BeanUtils.copyProperties(toOrderSubscribeCreateBizDto(createVo, goods), createBizDto);
		// 父订单号
		createBizDto.setParentOrderCode(createVo.getOrderCode());
		// 续订无保证金
		createBizDto.setDeposit(BigDecimal.ZERO);
		return createBizDto;
	}

	public static OrderTradeCancelModifyBizDto toOrderCancelModifyBizDto(TradeCancelModifyVo modifyVo) {
		OrderTradeCancelModifyBizDto beanBizDto = null;
		if (modifyVo != null) {
			beanBizDto = new OrderTradeCancelModifyBizDto();
			BeanUtils.copyProperties(modifyVo, beanBizDto);
			beanBizDto.setUserId(modifyVo.getOperateId());
			beanBizDto.setModifiedType(OrderStatusFlowModifiedTypeEnum.C_USER);
		}
		return beanBizDto;
	}

	public static OrderAioVo toOrderAioVo(OrderExclusivelBizDto orderBizDto, StoreBaseBizDto placeStore,
			UserVo user, VehicleNameBizDto vehicleBizDto, CarDetailBizDto carDetailBizDto, Boolean isPlaceStore,
			Integer repayPeriods, Integer waitPeriods, boolean isOverdue) {
		OrderAioVo beanVo = new OrderAioVo();
		// 订单号
		beanVo.setOrderCode(orderBizDto.getOrderCode());
		// 业务类型 2：专属
		beanVo.setBusinessType(orderBizDto.getType().getValue());
		// 订单状态
		beanVo.setOrderStatus(orderBizDto.getOrderStatus().getValue());
		beanVo.setOrderStatusStr(orderBizDto.getOrderStatus().getName());
		// 订单说明
		beanVo.setOrderExplain(OrderUtil.getOrderExplain(orderBizDto.getOrderStatus(), isPlaceStore));
		if (isPlaceStore) {
			if (!OrderExclusivelStatusEnum.TRADE_UNDELIVERY.equals(orderBizDto.getOrderStatus())) {
				beanVo.setServiceExplain(OrderConst.AIO_ORDER_SERVICE_EXPLAIN);
			}
		} else {
			beanVo.setServiceExplain(OrderConst.AIO_ORDER_SERVICE_EXPLAIN);
		}
		// 车辆图片
		beanVo.setCarImg(vehicleBizDto.getWhiteFigureTerminal());
		// 商品名称（品牌+车系+年款+车型名称）
		beanVo.setGoodsName(vehicleBizDto.getSubBrandName() + " " + vehicleBizDto.getSeriesName() + " "
				+ vehicleBizDto.getYear() + " " + vehicleBizDto.getModelName());
		// 当前时间
		beanVo.setCurrentDate(LocalDateTime.now());
		// 订单过期时间(待付款使用)
		beanVo.setExpiryDate(
				DateUtil.plus(orderBizDto.getOrderTime(), OrderConst.ORDER_VALID_TIME, ChronoUnit.MINUTES));
		List<DataVo> datas = new ArrayList<>();
		datas.add(new DataVo("订单编号", orderBizDto.getOrderCode() + ""));
		datas.add(new DataVo("下单时间", DateUtil.formatTime(orderBizDto.getOrderTime())));
		// （专属：品牌+车型名称+用车方案（回购/不回购）/租期（XX期））
		datas.add(new DataVo("交易商品", vehicleBizDto.getSubBrandName() + " " + vehicleBizDto.getModelName() + " "
				+ ((orderBizDto.getIsBuyback() ? "回购" : "不回购") + " / " + orderBizDto.getTransportTime() + "期")));
		if (OrderPaymentMethodEnum.INSTALLMENT.equals(orderBizDto.getPaymentMethod())) {
			// 分期
			datas.add(new DataVo("付款方式", "月付"));
			if (OrderExclusivelStatusEnum.TRADE_UNPAID.equals(orderBizDto.getOrderStatus())) {
				// 待付款
				datas.add(new DataVo("交易总额", BigDecimalUtils.toString(orderBizDto.getCartFee().add(orderBizDto.getDeposit())) + "元 (含保证金"
						+ orderBizDto.getDeposit() + "元)"));
				datas.add(new DataVo("应付金额", BigDecimalUtils.toString(orderBizDto.getRental().add(orderBizDto.getDeposit())) + "元 (含保证金"
						+ orderBizDto.getDeposit() + "元)"));
			} else {
				datas.add(new DataVo("已还期数", repayPeriods + "期"));
				if (isOverdue) {
					// 当前已逾期
					datas.add(new DataVo("待还期数", false, waitPeriods + "期", false, "(已逾期)", true));
				} else {
					datas.add(new DataVo("待还期数", waitPeriods + "期"));
				}
			}
		} else if (OrderPaymentMethodEnum.FULL.equals(orderBizDto.getPaymentMethod())) {
			// 全款
			datas.add(new DataVo("付款方式", "全款"));
			if (OrderExclusivelStatusEnum.TRADE_UNPAID.equals(orderBizDto.getOrderStatus())) {
				datas.add(new DataVo("应付金额", BigDecimalUtils.toString(orderBizDto.getCartFee().add(orderBizDto.getDeposit())) + "元 (含保证金"
						+ BigDecimalUtils.toString(orderBizDto.getDeposit()) + "元)"));
			}
		}
		datas.add(new DataVo("用户姓名", user.getRealName()));
		datas.add(new DataVo("身份证号", OrderUtil.desensitizeIdCard(user.getIdNumber())));
		datas.add(new DataVo("手机号码", OrderUtil.desensitizePhone(user.getPhone())));
		datas.add(new DataVo("下单渠道", OrderUtil.getOrderChannel(orderBizDto.getSource())));
		if (OrderExclusivelStatusEnum.TRADE_UNDELIVERY.equals(orderBizDto.getOrderStatus())) {
			datas.add(new DataVo("提车门店", placeStore.getShortName()));
		} else {
			datas.add(new DataVo("下单门店", placeStore.getShortName()));
		}
		datas.add(new DataVo("车 牌  号", carDetailBizDto == null ? "--" : carDetailBizDto.getLicencePlate()));
		datas.add(new DataVo("车 架  号", carDetailBizDto == null ? "--" : carDetailBizDto.getVin()));

		// 数据
		beanVo.setDatas(datas);
		return beanVo;
	}

	public static OrderAioVo toOrderAioVo(OrderSubscribeBizDto orderBizDto, StoreBaseBizDto placeStore, UserVo user,
			VehicleNameBizDto vehicleBizDto, Boolean isPlaceStore, Integer currentMileage,
			CarDetailBizDto carDetailBizDto) {
		OrderAioVo beanVo = new OrderAioVo();
		// 订单号
		beanVo.setOrderCode(orderBizDto.getOrderCode());
		// 业务类型 3：订阅
		beanVo.setBusinessType(orderBizDto.getType().getValue());
		// 订单状态
		beanVo.setOrderStatus(orderBizDto.getOrderStatus().getValue());
		beanVo.setOrderStatusStr(orderBizDto.getOrderStatus().getName());
		// 订单说明
		beanVo.setOrderExplain(OrderUtil.getOrderExplain(orderBizDto.getOrderStatus(), isPlaceStore));
		if (isPlaceStore) {
			if (!OrderExclusivelStatusEnum.TRADE_UNDELIVERY.equals(orderBizDto.getOrderStatus())) {
				beanVo.setServiceExplain(OrderConst.AIO_ORDER_SERVICE_EXPLAIN);
			}
		} else {
			beanVo.setServiceExplain(OrderConst.AIO_ORDER_SERVICE_EXPLAIN);
		}
		// 车辆图片
		beanVo.setCarImg(vehicleBizDto.getWhiteFigureTerminal());
		// 商品名称（品牌+车系+年款+车型名称）
		beanVo.setGoodsName(vehicleBizDto.getSubBrandName() + " " + vehicleBizDto.getSeriesName() + " "
				+ vehicleBizDto.getYear() + " " + vehicleBizDto.getModelName());
		// 当前时间
		beanVo.setCurrentDate(LocalDateTime.now());
		// 订单过期时间(待付款使用)
		beanVo.setExpiryDate(
				DateUtil.plus(orderBizDto.getOrderTime(), OrderConst.ORDER_VALID_TIME, ChronoUnit.MINUTES));
		
		List<DataVo> datas = new ArrayList<>();
		datas.add(new DataVo("订单编号", orderBizDto.getOrderCode() + ""));
		datas.add(new DataVo("下单时间", DateUtil.formatTime(orderBizDto.getOrderTime())));
		datas.add(new DataVo("交易商品", vehicleBizDto.getSubBrandName() + " " + vehicleBizDto.getModelName()));
		// 判断是否已超期
		long beyondDays = DateUtil.getDaysDiff(orderBizDto.getLeaseExpire(), LocalDate.now());
		if (beyondDays > 0L) {
			// 超期
			datas.add(new DataVo("用车时长", false, orderBizDto.getTransportTime() + "个月", false, "(已超" + beyondDays + "天)",
					true));
		} else {
			datas.add(new DataVo("用车时长", orderBizDto.getTransportTime() + "个月"));
		}
		// 总购买里程数
		Integer mileage = orderBizDto.getBaseMileage() + orderBizDto.getPackageMileage() + orderBizDto.getGiftMileage();
		if (orderBizDto.getPurchaseMileage() != null) {
			mileage += orderBizDto.getPurchaseMileage();
		}
		datas.add(new DataVo("购买里程", mileage + "公里(含赠送和加购里程)"));
		if (OrderSubscribeStatusEnum.TRADE_UNPAID.equals(orderBizDto.getOrderStatus())) {
			datas.add(
					new DataVo("应付金额", BigDecimalUtils.toString(orderBizDto.getCartFee().add(orderBizDto.getDeposit()))
							+ "(含保证金" + BigDecimalUtils.toString(orderBizDto.getDeposit()) + "元)"));
		} else if (currentMileage == null) {
			datas.add(new DataVo("行驶里程", "--"));
		} else {
			Integer travelMileage = currentMileage - orderBizDto.getStartMileage();
			if (currentMileage > orderBizDto.getWarningMileage()) {
				// 超里程
				datas.add(new DataVo("行驶里程", false, travelMileage + "公里", false,
						"(已超" + (currentMileage - orderBizDto.getWarningMileage()) + "公里)", true));
			} else {
				datas.add(new DataVo("行驶里程", travelMileage + "公里"));
			}
		}
		datas.add(new DataVo("用户姓名", user.getRealName()));
		datas.add(new DataVo("身份证号", OrderUtil.desensitizeIdCard(user.getIdNumber())));
		datas.add(new DataVo("手机号码", OrderUtil.desensitizePhone(user.getPhone())));
		datas.add(new DataVo("下单渠道", OrderUtil.getOrderChannel(orderBizDto.getSource())));
		if (OrderSubscribeStatusEnum.TRADE_UNDELIVERY.equals(orderBizDto.getOrderStatus())) {
			datas.add(new DataVo("提车门店", placeStore.getShortName()));
		} else {
			datas.add(new DataVo("下单门店", placeStore.getShortName()));
		}
		datas.add(new DataVo("车 牌  号", carDetailBizDto == null ? "--" : carDetailBizDto.getLicencePlate()));
		datas.add(new DataVo("车 架  号", carDetailBizDto == null ? "--" : carDetailBizDto.getVin()));

		// 数据
		beanVo.setDatas(datas);
		
		return beanVo;
	}

	public static DeliveryModifyBizDto toDeliveryModifyBizDto(TradeDeliveryModifyVo modifyVo, StoreBizDto store) {
		DeliveryModifyBizDto modifyDto = null;
		if (modifyVo != null) {
			modifyDto = new DeliveryModifyBizDto();
			modifyDto.setCarId(modifyVo.getCarId());
			// 交接信息信息
			modifyDto.setHandover(toHandoverCreateBizDto(modifyVo, store));
		}
		return modifyDto;
	}

	private static HandoverCreateBizDto toHandoverCreateBizDto(TradeDeliveryModifyVo modifyVo, StoreBizDto store) {
		HandoverCreateBizDto createDto = null;
		if (modifyVo != null) {
			createDto = new HandoverCreateBizDto();
			createDto.setStoreId(store.getStoreId());
			createDto.setDealerId(store.getDealerId());
			createDto.setCarId(modifyVo.getCarId());
			createDto.setOrderCode(modifyVo.getOrderCode());
			createDto.setMileage(modifyVo.getMileage());
			createDto.setKeyNums(modifyVo.getHandover().getKeyNums());
			createDto.setCheckUrl(modifyVo.getHandover().getCheckUrl());
			createDto.setCheckInfo(modifyVo.getHandover().getHandoverCheck());
			createDto.setType(HandoverTypeEnum.DELIVERY);
		}
		return createDto;
	}

	public static ReturnCarModifyBizDto toReturnCarModifyBizDto(ReturnCarModifyVo modifyVo, CarDto carDto,
			StoreBizDto store, CarCheckTypeEnum carCheckType) {
		ReturnCarModifyBizDto modifyDto = null;
		if (modifyVo != null) {
			modifyDto = new ReturnCarModifyBizDto();
			modifyDto.setCarId(modifyVo.getCarId());
			modifyDto.setCheckType(carCheckType);
			// 车辆类型
			modifyDto.setCarType(CarsTypeEnum.getEnum(carDto.getVehicleType()));
			// 交接信息信息
			modifyDto.setHandover(toHandoverCreateBizDto(modifyVo, store));
		}
		return modifyDto;
	}

	private static HandoverCreateBizDto toHandoverCreateBizDto(ReturnCarModifyVo modifyVo, StoreBizDto store) {
		HandoverCreateBizDto createDto = null;
		if (modifyVo != null) {
			createDto = new HandoverCreateBizDto();
			createDto.setStoreId(store.getStoreId());
			createDto.setDealerId(store.getDealerId());
			createDto.setCarId(modifyVo.getCarId());
			createDto.setOrderCode(modifyVo.getOrderCode());
			createDto.setMileage(modifyVo.getMileage());
			createDto.setKeyNums(modifyVo.getHandover().getKeyNums());
			createDto.setCheckUrl(modifyVo.getHandover().getCheckUrl());
			createDto.setCheckInfo(modifyVo.getHandover().getHandoverCheck());
			createDto.setType(HandoverTypeEnum.DELIVERY);
		}
		return createDto;
	}

	public static OrderTradeDeliveryModifyBizDto toOrderDeliveryModifyBizDto(TradeDeliveryModifyVo modifyVo,
			Integer warningMileage, Integer storeId) {
		OrderTradeDeliveryModifyBizDto modifyBizDto = null;
		if (modifyVo == null) {
			return modifyBizDto;
		}
		modifyBizDto = new OrderTradeDeliveryModifyBizDto();
		BeanUtils.copyProperties(modifyVo, modifyBizDto);
		// 大屏cpu
		modifyBizDto.setCpuSerial(modifyVo.getDeviceNum());
		// 门店id
		modifyBizDto.setStoreId(storeId);
		// 订单初始里程
		modifyBizDto.setStartMileage(modifyVo.getMileage());
		modifyBizDto.setWarningMileage(warningMileage);

		return modifyBizDto;
	}

	public static PurchaseVo toPurchaseVo(OrderMileageDto beanDto) {
		PurchaseVo beanVo = null;
		if (beanDto == null) {
			return beanVo;
		}
		beanVo = new PurchaseVo();
		BeanUtils.copyProperties(beanDto, beanVo);
		beanVo.setPayType(PaymentCostTypeEnum.PURCHASE.getValue());
		// 金额（加购金额+超里程金额）
		beanVo.setPurchaseAmount(beanDto.getPurchaseFee().add(beanDto.getBeyondFee()));
		beanVo.setPurchaseAmountStr(beanVo.getPurchaseAmount().stripTrailingZeros().toPlainString() + "元");
		// 加购里程
		beanVo.setPurchaseMileage(beanDto.getPurchaseMileage());
		beanVo.setPurchaseMileageStr(beanDto.getPurchaseMileage() + "公里");
		beanVo.setPurchaseStatus(beanDto.getStatus().getValue());
		beanVo.setPurchaseStatusStr("待付款");
		List<ButtonVo> buttons = new ArrayList<>();
		ButtonVo button = Convert.toButton(OrderButtonEnum.CANCEL);
		button.setName(button.getName().split("/")[0]);
		buttons.add(button);
		buttons.add(Convert.toButton(OrderButtonEnum.PAY));
		beanVo.setButtons(buttons);
		
		return beanVo;
	}

	public static ReportModifyBizDto toReportModifyBizDto(ReportModifyVo vo) {
		ReportModifyBizDto dto=null;
		if(null != vo) {
			dto=new ReportModifyBizDto();
			BeanUtils.copyProperties(vo,dto);
		}
		return dto;
	}

	public static CheckReportVo toCheckReportVo(PdiCheckBizDto pdiCheckBizDto, OrderSubscribeSettlementDto orderSubscribeSettlement) {
		CheckReportVo vo=new CheckReportVo();
		if(null != pdiCheckBizDto) {
			BeanUtils.copyProperties(pdiCheckBizDto,vo);
		}
		if(null != orderSubscribeSettlement){
			BeanUtils.copyProperties(orderSubscribeSettlement,vo);
		}
		return vo;
	}

	public static OrderReturnCarModifyBizDto toOrderReturnCarModifyBizDto(ReturnCarModifyVo modifyVo, StoreBizDto store) {
		OrderReturnCarModifyBizDto modifyDto = new OrderReturnCarModifyBizDto();
		BeanUtils.copyProperties(modifyVo, modifyDto);
		// 订单号
		modifyDto.setOrderCode(modifyVo.getOrderCode());
		// 订单还车时车辆里程
		modifyDto.setCurrentMileage(modifyVo.getMileage());
		// 门店id
		modifyDto.setStoreId(store.getStoreId());
		// 销售顾问id
		modifyDto.setSalesmanId(modifyVo.getSalesmanId());
		// 销售顾问姓名
		modifyDto.setSalesmanName(modifyVo.getSalesmanName());
		// 销售顾问身份证号
		modifyDto.setSalesmanIdNumber(modifyVo.getSalesmanIdNumber());
		// 还车终端cpu
		modifyDto.setCpuSerial(modifyVo.getDeviceNum());
		// 超里程公里单价
		modifyDto.setBeyondMileageUnitPrice(new BigDecimal(OrderConst.MILEAGE_UNIT_PRICE));
		// 超期天数单价
		modifyDto.setBeyondDayUnitPrice(new BigDecimal(OrderConst.DAY_UNIT_PRICE));

		return modifyDto;
	}

	public static OrderRenegeCancelModifyBizDto toOrderRenegeCancelModifyBizDto(RenegeCancelModifyVo modifyVo) {
		OrderRenegeCancelModifyBizDto modifyBizDto = null;
		if (modifyVo != null) {
			modifyBizDto = new OrderRenegeCancelModifyBizDto();
			BeanUtils.copyProperties(modifyVo, modifyBizDto);
		}
		return modifyBizDto;
	}

	public static OrderPayeeVo toOrderPayeeVo(OrderRenegeBizDto beanBizDto) {
		OrderPayeeVo beanVo = null;
		if (beanBizDto != null) {
			beanVo = new OrderPayeeVo();
			BeanUtils.copyProperties(beanBizDto, beanVo);
		}
		return beanVo;
	}

	public static OrderPayeeVo toOrderPayeeVo(UserPayeeDto beanDto) {
		OrderPayeeVo beanVo = null;
		if (beanDto != null) {
			beanVo = new OrderPayeeVo();
			BeanUtils.copyProperties(beanDto, beanVo);
		}
		return beanVo;
	}

	public static UserPayeeCreateDto toUserPayeeCreateDto(OrderPayeeCreateVo createVo) {
		UserPayeeCreateDto createDto = null;
		if (createVo != null) {
			createDto = new UserPayeeCreateDto();
			BeanUtils.copyProperties(createVo, createDto);
		}

		return createDto;
	}

	public static UserPayeeModifyDto toUserPayeeModifyDto(OrderPayeeCreateVo createVo) {
		UserPayeeModifyDto modifyDto = null;
		if (createVo != null) {
			modifyDto = new UserPayeeModifyDto();
			BeanUtils.copyProperties(createVo, modifyDto);
		}

		return modifyDto;
	}

	public static OrderRenegePayeeModifyBizDto toOrderRenegePayeeModifyBizDto(OrderPayeeCreateVo createVo) {
		OrderRenegePayeeModifyBizDto modifyBizDto = null;
		if (createVo != null) {
			modifyBizDto = new OrderRenegePayeeModifyBizDto();
			BeanUtils.copyProperties(createVo, modifyBizDto);
		}

		return modifyBizDto;
	}
}
